package com.lzm.ydpt.module.order.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.base.TabFragmentAdapter;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonwidget.NoScrollViewPager;
import com.byt.framlib.commonwidget.StatusBarUtil;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.Dapplication;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.UserInfo;
import com.lzm.ydpt.module.order.fragment.UserHomeFragment;
import com.lzm.ydpt.module.order.fragment.UserMineFragment;
import com.lzm.ydpt.module.order.fragment.UserOrderFragment;
import com.lzm.ydpt.module.order.utils.OnPageCallBack;
import com.lzm.ydpt.mvp.contract.PtMainContract;
import com.lzm.ydpt.mvp.presenterimpl.PtMainPresenterImpl;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 跑腿首页
 */
public class UserMainActivity extends BaseActivity<PtMainPresenterImpl> implements View.OnClickListener, PtMainContract.View {

    @BindView(R.id.nsvp_main_context)
    NoScrollViewPager nsvp_main_context;

    private RelativeLayout[] arrayView;
    private ImageView[] arrayImg;
    private TextView[] arrayTV;
    private ArrayList<BaseFragment> fragmentList = new ArrayList<>();
    private TabFragmentAdapter mAdapters;

    private PtMainContract.Presenter mPresenter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        isShow = false;
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_riding_main;
    }

    @Override
    public PtMainPresenterImpl initPreData() {
        return new PtMainPresenterImpl(this);
    }

    @Override
    public void initView() {
        initWidget();
//        fetchUser();
    }

    private void fetchUser() {
        FormBodys formBodys = new FormBodys.Builder()
                .add("memberId", 1)
                .build();
        mPresenter.getUserInfo(formBodys);
    }

    private UserHomeFragment mUserHomeFragment = null;
    private UserOrderFragment mUserOrderFragment = null;
    private UserMineFragment mUserMineFragment = null;

    @SuppressLint("NewApi")
    private void initWidget() {
        arrayImg = new ImageView[3];
        arrayView = new RelativeLayout[3];
        arrayTV = new TextView[3];
        arrayImg[0] = findViewById(R.id.img_main_home);
        arrayImg[1] = findViewById(R.id.img_main_oder);
        arrayImg[2] = findViewById(R.id.img_main_mine);
        arrayView[0] = findViewById(R.id.rl_main_home);
        arrayView[1] = findViewById(R.id.rl_main_oder);
        arrayView[2] = findViewById(R.id.rl_main_mine);
        arrayTV[0] = findViewById(R.id.tv_main_home);
        arrayTV[1] = findViewById(R.id.tv_main_order);
        arrayTV[2] = findViewById(R.id.tv_main_mine);
        mUserHomeFragment = UserHomeFragment.newsInstance();
        fragmentList.add(mUserHomeFragment);
        mUserOrderFragment = UserOrderFragment.newsInstance();
        fragmentList.add(mUserOrderFragment);
        mUserMineFragment = UserMineFragment.newsInstance(new OnPageCallBack() {
            @Override
            public void pageCallBack(int page) {
                setCurrentSelect(page);
            }
        });
        fragmentList.add(mUserMineFragment);
        mAdapters = new TabFragmentAdapter(getSupportFragmentManager(), fragmentList);
        nsvp_main_context.setAdapter(mAdapters);
        nsvp_main_context.setOffscreenPageLimit(fragmentList.size());
        for (int i = 0, length = arrayView.length; i < length; i++) {
            arrayView[i].setOnClickListener(this);
        }
        setCurrentSelect(0);
    }

    private void setCurrentSelect(int current) {
        nsvp_main_context.setCurrentItem(current, false);
        for (int i = 0, length = arrayImg.length; i < length; i++) {
            arrayImg[i].setSelected(current == i);
            arrayTV[i].setSelected(current == i);
        }
    }

    @OnClick({R.id.rl_main_home, R.id.rl_main_oder, R.id.rl_main_mine})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_main_home:
                setCurrentSelect(0);
                changStatusIconCollor(true);
                break;
            case R.id.rl_main_oder:
                setCurrentSelect(1);
                changStatusIconCollor(true);
                break;
            case R.id.rl_main_mine:
                setCurrentSelect(2);
                changStatusIconCollor(false);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void showMessage(String message, String method) {
        stopProgressDialog();
        showShortToast(message);
    }

    @Override
    public void showErrorView(String msg) {
        stopProgressDialog();
        showShortToast(msg);
    }

    @Override
    public void onUserInfo(UserInfo mUserInfo) {
        if (mUserInfo != null) {
            Dapplication.setUserInfo(mUserInfo);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case UserHomeFragment.GRATUITY_CODE:
                    float gratuity = data.getFloatExtra(Constants.INP_GRATUITY_VALUE, 0f);
                    if (mUserHomeFragment != null) {
                        mUserHomeFragment.setGratuityValue(gratuity);
                    }
                    break;
            }
        }
    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.setTranslucentForImageViewInFragment(UserMainActivity.this, null);
        changStatusIconCollor(true);
    }
}